<?php
/*
  功能：用户抽奖记录 model
  作者：cxm
  时间：2019-10-23
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserDrawRecord extends BaseModel {
    use SoftDeletes;
    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='user_draw_record';  //绑定表

    /**
     * 用户
     */
    public function user(){
        return $this->hasOne(User::class,'id','user_id');
    }

    /**
     * 活动
     */
    public function activity(){
        return $this->hasOne(Activity::class,'id','activity_id');
    }

    /**
     * 获取当天抽奖次数
     * @param $type
     * @return mixed
     */
    public static function getDateNum($type){
        $date = date('Y-m-d', time());
        return self::whereDate('created_at',$date)->count();
    }

    /**
     * 用户当日抽奖次数
     * @param $user_id
     * @return mixed
     */
    public static function isUse($user_id){
        return self::where('user_id',$user_id)->whereDate('created_at',date('Y-m-d'))->count();
    }

    /**
     * 获取中奖数量
     * @param $activity_id 活动ID
     * @return mixed
     */
    public static function getActivityPrizeCount($activity_id){
        return self::where('activity_id',$activity_id)->count();
    }
}